package cn.xm.pet.service.impl;

import cn.xm.basic.util.AjaxResult;
import cn.xm.pet.domain.Product;
import cn.xm.pet.domain.ProductLog;
import cn.xm.pet.mapper.ProductLogMapper;
import cn.xm.pet.mapper.ProductMapper;
import cn.xm.pet.service.ProductService;
import cn.xm.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xm
 * @since 2022-11-03
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductLogMapper productLogMapper;

    @Override
    public void offsale(List<Long> ids,Long auditId) {
        // 找到传入Id对应的服务
        List<Product> list = productMapper.findByIds(ids);
        // 取出上架状态的
        List<Long> onsaleIds = list.stream().filter(product -> product.getState() == 1).map(Product::getId).collect(Collectors.toList());
        productMapper.offsale(onsaleIds);
        // 记录日志
        ProductLog productLog = new ProductLog();
        for (Long onsaleId : onsaleIds) {
            productLog.setProductId(onsaleId);
            productLog.setProductState(0);
            productLog.setAuditId(auditId);
            productLogMapper.add(productLog);
        }

        //        for (Product product : products) {
//            product.setOffsaletime(new Date());
//            productMapper.update(product);
//        }
    }

    @Override
    public void onsale(List<Long> ids, Long auditId) {
        // 找到传入Id对应的服务
        List<Product> list = productMapper.findByIds(ids);
        // 取出下架状态的
        List<Long> offsaleIds = list.stream().filter(product -> product.getState() == 0).map(Product::getId).collect(Collectors.toList());
        productMapper.onsale(offsaleIds);
        // 记录日志
        ProductLog productLog = new ProductLog();
        for (Long offsaleId : offsaleIds) {
            productLog.setProductId(offsaleId);
            productLog.setProductState(1);
            productLog.setAuditId(auditId);
            productLogMapper.add(productLog);
        }

    }

    @Override
    public Product findDetailByProductId(Long id) {
        return productMapper.findDetailByProductId(id);
    }




}
